﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

using EdgeStatz.ESSystems.Objects;

namespace EdgeStatz.ModelEngine.ReportTools
{
    class ReportToolsXml
    {
        ES_R_ReportCollection reports = new ES_R_ReportCollection();

        private string xmlFilePath;
        private string modelname;
        private string modelpath;

        public string ModelPath
        {
            get
            {
                if (!modelpath.EndsWith("\\"))
                    modelpath += "\\";
                return modelpath;
            }
        }

        public string ModelName
        {
            get { return modelname; }
        }

        public string[] ReportNames
        {
            get 
            {
                List<string> names = new List<string> { };
                foreach (ES_R_Report report in reports)
                    names.Add(report.Name);
                return names.ToArray(); 
            }
        }

        public string[] FileNames
        {
            get 
            {
                List<string> files = new List<string> { };
                foreach (ES_R_Report report in reports)
                    files.Add(report.Path); 
                return files.ToArray();
            }
        }

        public ES_R_ReportCollection Reports { get { return reports; } }

        public string GetReportName(int index)
        {
            if (reports.Count < index)
                return null;
            return reports[index].Name;
        }

        public string GetFileName(int index)
        {
            if (reports.Count < index)
                return null;
            return reports[index].Path;
        }

        public string GetFileName(string reportName)
        {
            int index = -1;
            for(int i=0;i<reports.Count;i++)
                if(reports[i].Name.ToUpper().Equals(reportName.ToUpper()))
                    index = i;
            if (index > -1)
                return (GetFileName(index));
            else
                return null;
        }

        public ReportToolsXml(string path)
        {
            if (path == null)
                path = GlobalSystemSettings.ModelFilePath;
            using (XmlTextReader xmlReader = new XmlTextReader(path))
                xmlFilePath = xmlReader.BaseURI;
            Read();
        }

        private void Read()
        {
            XmlDocument myXmlDocument = new XmlDocument();
            myXmlDocument.Load(xmlFilePath);

            XmlNode rootNode;
            rootNode = myXmlDocument.DocumentElement;
            modelname = rootNode.Attributes["Name"].Value;
            modelpath = rootNode.Attributes["Path"].Value;

            reports.Clear();

            foreach (XmlNode node1 in rootNode.ChildNodes)
            {
                if (node1.Name.ToUpper() == "REPORTS")
                {
                    foreach (XmlNode node2 in node1.ChildNodes)
                    {
                        if (node2.Name.ToUpper() == "REPORT")
                        {
                            if (node2.Attributes["Name"] == null)
                                throw new Exception("Name tag is required for Report controls.");
                            if (node2.Attributes["File"] == null)
                                throw new Exception("File tag is required for Report controls.");
                            string name = node2.Attributes["Name"].Value;
                            string file = node2.Attributes["File"].Value;
                            List<string> ds = new List<string> { };
                            foreach (XmlNode node3 in node2.ChildNodes)
                            {
                                if (node3.Name.ToUpper().Equals("DATASOURCE"))
                                {
                                    ds.Add(node3.InnerText);
                                }
                            }
                            reports.Add(GetReportData(name, modelpath + (modelpath.EndsWith("\\") ? "" : "\\") + file, ds));
                        }
                    }
                }
            }
        }

        private ES_R_Report GetReportData(string name, string filename, List<string> ds2r )
        {
            ES_R_Report report = new ES_R_Report();
            report.Name = name;
            report.Path = filename;
            report.DataSetsToRun = ds2r;

            XmlDocument myXmlDocument = new XmlDocument();
            myXmlDocument.Load(filename);
            XmlNode rootNode;
            rootNode = myXmlDocument.DocumentElement;

            foreach (XmlNode node1 in rootNode.ChildNodes)
            {
                if (node1.Name.ToUpper().Equals("DATASOURCES"))
                {
                    foreach (XmlNode node2 in node1.ChildNodes)
                    {
                        if (node2.Name.ToUpper().Equals("DATASOURCE"))
                        {
                            ES_R_DataSource data_source = new ES_R_DataSource();
                            if (node2.Attributes["Name"] != null)
                                data_source.Name = node2.Attributes["Name"].Value;
                            foreach (XmlNode node3 in node2.ChildNodes)
                            {
                                if (node3.Name.ToUpper().Equals("CONNECTIONPROPERTIES"))
                                {
                                    foreach (XmlNode node4 in node3.ChildNodes)
                                    {
                                        if (node4.Name.ToUpper().Equals("DATAPROVIDER"))
                                            data_source.Data_Provider = node4.InnerText;
                                        else if (node4.Name.ToUpper().Equals("CONNECTSTRING"))
                                            data_source.LoadConnectionString(node4.InnerText);
                                    }
                                }
                            }
                            report.DataSources.Add(data_source);
                        }
                    }
                }
                else if (node1.Name.ToUpper().Equals("DATASETS"))
                {
                    foreach (XmlNode node2 in node1.ChildNodes)
                    {
                        if (node2.Name.ToUpper().Equals("DATASET"))
                        {
                            ES_R_DataSet data_set = new ES_R_DataSet();
                            if (node2.Attributes["Name"] != null)
                                data_set.Name = node2.Attributes["Name"].Value;
                            foreach (XmlNode node3 in node2.ChildNodes)
                            {
                                if (node3.Name.ToUpper().Equals("FIELDS"))
                                {
                                    foreach (XmlNode node4 in node3.ChildNodes)
                                    {
                                        if (node4.Name.ToUpper().Equals("FIELD"))
                                        {
                                            ES_R_DataField field = new ES_R_DataField();
                                            field.Name = node4.Attributes["Name"] == null ? "" : node4.Attributes["Name"].Value;
                                            foreach (XmlNode node5 in node4.ChildNodes)
                                            {
                                                if (node5.Name.ToUpper().Equals("DATAFIELD"))
                                                    field.DataField = node5.InnerText;
                                                else if (node5.Name.ToUpper().Equals("RD:TYPENAME"))
                                                    field.Type = ES_R_ReportParameter.GetType(node5.InnerText);
                                            }
                                            data_set.Fields.Add(field);
                                        }
                                    }
                                }
                                else if (node3.Name.ToUpper().Equals("QUERY"))
                                {
                                    foreach (XmlNode node4 in node3.ChildNodes)
                                    {
                                        if (node4.Name.ToUpper().Equals("DATASOURCENAME"))
                                            data_set.DataSourceName = node4.InnerText;
                                        else if (node4.Name.ToUpper().Equals("COMMANDTEXT"))
                                            data_set.CommandText = node4.InnerText;
                                        else if (node4.Name.ToUpper().Equals("QUERYPARAMETERS"))
                                        {
                                            foreach (XmlNode node5 in node4.ChildNodes)
                                            {
                                                if (node5.Name.ToUpper().Equals("QUERYPARAMETER"))
                                                {
                                                    ES_R_QueryParameter parameter = new ES_R_QueryParameter();
                                                    parameter.Name = node5.Attributes["Name"] == null ? "" : node5.Attributes["Name"].Value;
                                                    foreach (XmlNode node6 in node5.ChildNodes)
                                                        if (node6.Name.ToUpper().Equals("VALUE"))
                                                            parameter.Value = node6.InnerText;
                                                    data_set.Parameters.Add(parameter);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            report.DataSets.Add(data_set);
                        }
                    }
                }
                else if (node1.Name.ToUpper().Equals("REPORTPARAMETERS"))
                {
                    foreach (XmlNode node2 in node1.ChildNodes)
                    {
                        if (node2.Name.ToUpper().Equals("REPORTPARAMETER"))
                        {
                            ES_R_ReportParameter r_parameter = new ES_R_ReportParameter();
                            r_parameter.Name = node2.Attributes["Name"] == null ? "" : node2.Attributes["Name"].Value;
                            foreach (XmlNode node3 in node2.ChildNodes)
                            {
                                if (node3.Name.ToUpper().Equals("DATATYPE"))
                                    r_parameter.Type = ES_R_ReportParameter.GetType(node3.InnerText);
                                else if (node3.Name.ToUpper().Equals("PROMPT"))
                                    r_parameter.Prompt = node3.InnerText;
                                else if (node3.Name.ToUpper().Equals("NULLABLE"))
                                    r_parameter.Nullable = node3.InnerText.ToLower().Equals("true") ? true : false;
                                else if (node3.Name.ToUpper().Equals("ALLOWBLANK"))
                                    r_parameter.AllowBlank = node3.InnerText.ToLower().Equals("true") ? true : false;
                                else if (node3.Name.ToUpper().Equals("VALIDVALUES"))
                                {
                                    foreach (XmlNode node4 in node3.ChildNodes)
                                    {
                                        if (node4.Name.ToUpper().Equals("DATASETREFERENCE"))
                                        {
                                            r_parameter.ParameterType = ES_R_ParameterTypes.DataSetReference;
                                            foreach (XmlNode node5 in node4.ChildNodes)
                                            {
                                                if (node5.Name.ToUpper().Equals("DATASETNAME"))
                                                    r_parameter.Dataset_Name = node5.InnerText;
                                                else if (node5.Name.ToUpper().Equals("VALUEFIELD"))
                                                    r_parameter.Dataset_ValueField = node5.InnerText;
                                                else if (node5.Name.ToUpper().Equals("LABELFIELD"))
                                                    r_parameter.Dataset_LabelField = node5.InnerText;
                                            }
                                        }
                                    }
                                }
                            }
                            report.ReportParameters.Add(r_parameter);
                        }
                    }
                }
            }
            return report;
        }
    }
}
